package com.suzao.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @classname: RabbitConfig
 * @description:
 * @author: mc
 * @create: 2020-10-29 16:10
 **/
@Configuration
@PropertySource("classpath:rabbitmq.properties")
public class RabbitConfig {
    @Value("${com.suzao.firstqueue}")
    private String firstQueue;
    @Value("${com.suzao.secondqueue}")
    private String secondqueue;
    @Value("${com.suzao.thirdqueue}")
    private String thirdqueue;
    @Value("${com.suzao.fourthqueue}")
    private String fourthqueue;

    @Value("${com.suzao.directexchange}")
    private String directexchange;
    @Value("${com.suzao.topicexchange}")
    private String topicexchange;
    @Value("${com.suzao.fanoutexchange}")
    private String fanoutexchange;

    @Bean("szFirstQueue")
    public Queue getFirstQueue(){
        return  new Queue(firstQueue);
    }
    @Bean("szSecondQueue")
    public Queue getSecondqueue(){
        return  new Queue(secondqueue);
    }
    @Bean("szThirdQueue")
    public Queue getThirdqueue(){
        return  new Queue(thirdqueue);
    }
    @Bean("szFourthQueue")
    public Queue getFourthqueue(){
        return  new Queue(fourthqueue);
    }

    @Bean("szDirectExchange")
    public DirectExchange getDirectExchange(){
        return new DirectExchange(directexchange);
    }
    @Bean("szTopicExchange")
    public TopicExchange getTopicExchange(){
        return new TopicExchange(topicexchange);
    }
    @Bean("szFanoutExchange")
    public FanoutExchange getFanoutExchange(){
        return new FanoutExchange(fanoutexchange);
    }
    // 定义四个绑定关系
    @Bean
    public Binding bindFirst(@Qualifier("szFirstQueue") Queue queue, @Qualifier("szDirectExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("suzao.best");
    }

    @Bean
    public Binding bindSecond(@Qualifier("szSecondQueue") Queue queue, @Qualifier("szTopicExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("*.suzao.*");
    }

    @Bean
    public Binding bindThird(@Qualifier("szThirdQueue") Queue queue, @Qualifier("szFanoutExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public Binding bindFourth(@Qualifier("szFourthQueue") Queue queue, @Qualifier("szFanoutExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setAutoStartup(true);
        return factory;
    }



}
